package smecta.client.print.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import smecta.client.print.common.constants.ScaleConstant;
import smecta.client.print.domain.ScpSysSettings;
import smecta.client.print.service.ScpSysSettingsService;
import smecta.client.print.util.AlertUtil;

import java.util.Objects;
import java.util.Properties;

import smecta.client.print.util.weight.WeightSensor;

@Slf4j
@Component
public class ScaleSettingsController {

    @Autowired
    private ScpSysSettingsService scpSysSettingsService;

    @Autowired
    private WeightSensor weightSensor;

    @FXML
    private TextArea testArea1;

    @FXML
    private TextArea testArea2;

    @FXML
    private TextField dataFlag;

    @FXML
    private ComboBox<String> scaleName;

    @FXML
    private TextField readStartPos;

    @FXML
    private TextField port;

    @FXML
    private TextField readLength;

    @FXML
    private ComboBox<String> baudRate;

    @FXML
    private TextField multipleRelation;

    @FXML
    private ComboBox<String> dataBits;

    @FXML
    private TextField confirmTimes;

    @FXML
    private ComboBox<String> stopBits;

    @FXML
    private TextField readInterval;

    @FXML
    private ComboBox<String> parityCheck;

    @FXML
    private RadioButton reverseReading;

    @FXML
    private ComboBox<String> largeScaleType;

    @FXML
    private TextField largeScaleRange1;

    @FXML
    private TextField largeScaleRange2;

    @FXML
    private ComboBox<String> smallScaleType;

    @FXML
    private TextField smallScaleRange1;

    @FXML
    private TextField smallScaleRange2;

    @FXML
    private RadioButton conveyorMode;

    @FXML
    private TextField lineReadInterval;

    @FXML
    private TextField conveyorCount;

    @FXML
    private TextField decrementTimes;

    @FXML
    private TextField lineDetermineTimeLimit;

    @FXML
    private TextField plcPort;

    @FXML
    private TextField plcChar;

    @FXML
    private ComboBox<String> plcBaudRate;

    @FXML
    private ComboBox<String> plcParityCheck;

    @FXML
    private ComboBox<String> plcDataBits;

    @FXML
    private ComboBox<String> plcStopBits;

    @FXML
    private Button handleSaveSettings;

    @FXML
    private Button handleLoadSettings;

    @FXML
    private Button handleTest;

    @FXML
    private Button handleConveyorTest;

    private Properties properties = new Properties();
    private static final String KEY = ScaleConstant.DB_DICT_KEY;

    @FXML
    public void initialize() {
        scaleName.setItems(FXCollections.observableArrayList("T-2000A"));
        baudRate.setItems(FXCollections.observableArrayList("600", "1200", "2400", "3600", "9600"));
        dataBits.setItems(FXCollections.observableArrayList("5", "6", "7", "8"));
        stopBits.setItems(FXCollections.observableArrayList("1", "2"));
        parityCheck.setItems(FXCollections.observableArrayList("无", "奇校验", "偶校验"));
        largeScaleType.setItems(FXCollections.observableArrayList("装好称", "秤上装"));
        smallScaleType.setItems(FXCollections.observableArrayList("装好称", "秤上装"));
        plcBaudRate.setItems(FXCollections.observableArrayList("600", "1200", "2400", "3600", "9600"));
        plcParityCheck.setItems(FXCollections.observableArrayList("无", "奇校验", "偶校验"));
        plcDataBits.setItems(FXCollections.observableArrayList("5", "6", "7", "8"));
        plcStopBits.setItems(FXCollections.observableArrayList("1", "2"));

        handleLoadSettings();
    }

    @FXML
    private void handleSaveSettings() {
        try {
            properties.setProperty("testArea1", testArea1.getText());
            properties.setProperty("testArea2", testArea2.getText());
            properties.setProperty("dataFlag", dataFlag.getText());
            properties.setProperty("scaleName", scaleName.getValue());
            properties.setProperty("readStartPos", readStartPos.getText());
            properties.setProperty("port", port.getText());
            properties.setProperty("readLength", readLength.getText());
            properties.setProperty("baudRate", baudRate.getValue());
            properties.setProperty("multipleRelation", multipleRelation.getText());
            properties.setProperty("dataBits", dataBits.getValue());
            properties.setProperty("confirmTimes", confirmTimes.getText());
            properties.setProperty("stopBits", stopBits.getValue());
            properties.setProperty("readInterval", readInterval.getText());
            properties.setProperty("parityCheck", parityCheck.getValue());
            properties.setProperty("reverseReading", String.valueOf(reverseReading.isSelected()));
            properties.setProperty("largeScaleType", largeScaleType.getValue());
            properties.setProperty("largeScaleRange1", largeScaleRange1.getText());
            properties.setProperty("largeScaleRange2", largeScaleRange2.getText());
            properties.setProperty("smallScaleType", smallScaleType.getValue());
            properties.setProperty("smallScaleRange1", smallScaleRange1.getText());
            properties.setProperty("smallScaleRange2", smallScaleRange2.getText());
            properties.setProperty("conveyorMode", String.valueOf(conveyorMode.isSelected()));
            properties.setProperty("lineReadInterval", lineReadInterval.getText());
            properties.setProperty("conveyorCount", conveyorCount.getText());
            properties.setProperty("decrementTimes", decrementTimes.getText());
            properties.setProperty("lineDetermineTimeLimit", lineDetermineTimeLimit.getText());
            properties.setProperty("plcPort", plcPort.getText());
            properties.setProperty("plcChar", plcChar.getText());
            properties.setProperty("plcBaudRate", plcBaudRate.getValue());
            properties.setProperty("plcParityCheck", plcParityCheck.getValue());
            properties.setProperty("plcDataBits", plcDataBits.getValue());
            properties.setProperty("plcStopBits", plcStopBits.getValue());

            scpSysSettingsService.updateKeyOrInit(KEY, JSON.toJSONString(properties));
            AlertUtil.infoFinishAutoClose("保存成功", 3000L);
        } catch (Exception e) {
            log.error("handleSaveSettings error:{}", e);
        }
    }

    private Properties loadFromDB() {
        ScpSysSettings setting = scpSysSettingsService.findByKey(KEY);
        if (Objects.isNull(setting)) {
            return properties;
        }
        Properties currentProp = JSONObject.parseObject(setting.getSettingValue(), Properties.class);
        properties = currentProp;
        return properties;
    }

    @FXML
    private void handleLoadSettings() {
        try {
            loadFromDB();
            testArea1.setText(properties.getProperty("testArea1", ""));
            testArea2.setText(properties.getProperty("testArea2", ""));
            dataFlag.setText(properties.getProperty("dataFlag", ""));
            scaleName.setValue(properties.getProperty("scaleName", ""));
            readStartPos.setText(properties.getProperty("readStartPos", ""));
            port.setText(properties.getProperty("port", ""));
            readLength.setText(properties.getProperty("readLength", ""));
            baudRate.setValue(properties.getProperty("baudRate", ""));
            multipleRelation.setText(properties.getProperty("multipleRelation", ""));
            dataBits.setValue(properties.getProperty("dataBits", ""));
            confirmTimes.setText(properties.getProperty("confirmTimes", ""));
            stopBits.setValue(properties.getProperty("stopBits", ""));
            readInterval.setText(properties.getProperty("readInterval", ""));
            parityCheck.setValue(properties.getProperty("parityCheck", ""));
            reverseReading.setSelected(Boolean.parseBoolean(properties.getProperty("reverseReading", "false")));
            largeScaleType.setValue(properties.getProperty("largeScaleType", ""));
            largeScaleRange1.setText(properties.getProperty("largeScaleRange1", ""));
            largeScaleRange2.setText(properties.getProperty("largeScaleRange2", ""));
            smallScaleType.setValue(properties.getProperty("smallScaleType", ""));
            smallScaleRange1.setText(properties.getProperty("smallScaleRange1", ""));
            smallScaleRange2.setText(properties.getProperty("smallScaleRange2", ""));
            conveyorMode.setSelected(Boolean.parseBoolean(properties.getProperty("conveyorMode", "false")));
            lineReadInterval.setText(properties.getProperty("lineReadInterval", ""));
            conveyorCount.setText(properties.getProperty("conveyorCount", ""));
            decrementTimes.setText(properties.getProperty("decrementTimes", ""));
            lineDetermineTimeLimit.setText(properties.getProperty("lineDetermineTimeLimit", ""));
            plcPort.setText(properties.getProperty("plcPort", ""));
            plcChar.setText(properties.getProperty("plcChar", ""));
            plcBaudRate.setValue(properties.getProperty("plcBaudRate", ""));
            plcParityCheck.setValue(properties.getProperty("plcParityCheck", ""));
            plcDataBits.setValue(properties.getProperty("plcDataBits", ""));
            plcStopBits.setValue(properties.getProperty("plcStopBits", ""));
        } catch (Exception e) {
            log.error("handleLoadSettings error:{}", e);
        }
    }

    @FXML
    private void handleTest() {
        // 测试按钮的逻辑
        weightSensor.refreshWeightSensor(ScaleConstant.SENSOR_FUNCTION_CODE_001,this);

    }

    @FXML
    private void handleConveyorTest() {
        // 流线称重模式测试按钮的逻辑
    }


    public void updateTestArea1(String content) {
        if(StringUtils.isNotBlank(content)){
            testArea1.setText(content);
        }
    }
}
